package controller;

import pojo.Emp;
import service.IEmpService;
import service.impl.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

/*
* Servlet 技术    可以运行在服务器端，Servlet对象从创建到销毁借由服务器负责
* */
public class EmpController0 extends HttpServlet {

    private IEmpService ies = new EmpServiceImpl();

    @Override //处理get请求发送的数据
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override //处理post请求发送的数据
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

    @Override //请求分发：所有前端页面发送过来的请求都先到达该方法，然后再进行分发到指定方法里
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.service(req, resp);   //只根据请求的提交方式分发,现在需要根据请求路径分发

        // 获取请求的URI（例如：/app/deleteEmp）
        String requestURI = req.getRequestURI();
        // 找到最后一个"/"的位置并加1，获取方法名的起始位置
        int index=requestURI.lastIndexOf("/")+1;
        // 提取方法名（例如：deleteEmp）
        String mName = requestURI.substring(index);
        // 获取当前控制器类的Class对象
        Class<EmpController0> cla = EmpController0.class;

        Method method = null;
        try {
            // 通过反射获取对应的方法对象
            method = cla.getMethod(mName, HttpServletRequest.class, HttpServletResponse.class);
            // 调用目标方法，传入当前对象和请求/响应参数
            method.invoke(this,req,resp);

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }


    public void empdel(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{

        //设置响应信息的字符编码为utf-8
        response.setContentType("text/html;charset=utf-8");


        String empno = request.getParameter("empno");
        boolean b = ies.removeByEmpno(Integer.parseInt(empno));
        if (b) {
            response.getWriter().println("<script type='text/javascript'> alert('删除成功 ok');</script>");
        }else {
            response.getWriter().println("<script type='text/javascript'> alert('删除失败 not ok');</script>");
        }


        ArrayList<Emp> emps=ies.queryAll();
        response.getWriter().println("<html>");
        response.getWriter().println("<body>");
        response.getWriter().println("<table>");
        response.getWriter().println("<tr><td>员工编号</td><td>员工姓名</td><td>岗位名称</td><td>工资</td><td>操作</td></tr>");
        for (Emp e:emps){
            response.getWriter().println("<tr><td>"+e.getEmpno()+"</td><td>"+e.getEname()+"</td><td>"+e.getJob()+"</td><td>"+e.getSal()+"</td><td><a href='empdel?empno="+e.getEmpno()+"'>删除<a></td></tr>");
        }
        response.getWriter().println("</table>");
        response.getWriter().println("</body>");
        response.getWriter().println("</html>");
    }


    public void emplogin(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        /*super.doGet(request, response); 报405错误目的  告诉开发者必须对当前方法做重写*/

        //设置响应信息的字符编码为utf-8
        response.setContentType("text/html;charset=utf-8");


        //获得请求地址里的参数
        String empno = request.getParameter("empno");
        String ename = request.getParameter("ename");

        Emp login = ies.Login(new Emp(Integer.parseInt(empno),ename));

        if(login!=null){
            response.getWriter().println("<script type='text/javascript'> alert('登录成功 ok');</script>");

            ArrayList<Emp> emps=ies.queryAll();
            response.getWriter().println("<html>");
            response.getWriter().println("<body>");
            response.getWriter().println("<table>");
            response.getWriter().println("<tr><td>员工编号</td><td>员工姓名</td><td>岗位名称</td><td>工资</td><td>操作</td></tr>");
            for (Emp e:emps){
                response.getWriter().println("<tr><td>"+e.getEmpno()+"</td><td>"+e.getEname()+"</td><td>"+e.getJob()+"</td><td>"+e.getSal()+"</td><td><a href='empdel?empno="+e.getEmpno()+"'>删除<a></td></tr>");
            }
            response.getWriter().println("</table>");
            response.getWriter().println("</body>");
            response.getWriter().println("</html>");
        }else {
            response.getWriter().println("<script type='text/javascript'> alert('登录失败 not ok');</script>");
        }

    }
}

